package cn.sylinx.common.ext.map;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sylinx.common.lang.StringUtil;

@SuppressWarnings("rawtypes")
public final class MapUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static String getDateString(Map map, String key) {

		Date d = getDate(map, key);
		if (d == null) {
			return null;
		}

		return sdf.format(d);
	}

	public static Date getDate(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof Date) {
			return (Date) obj;
		}

		String objStr = obj.toString();
		if (StringUtil.isBlank(objStr)) {
			return null;
		}

		try {
			Date date = sdf.parse(objStr);
			return date;
		} catch (ParseException e) {
			return null;
		}

	}

	public static String getDatetimeString(Map map, String key) {

		Date d = getDatetime(map, key);
		if (d == null) {
			return null;
		}

		return sdf1.format(d);
	}

	public static Date getDatetime(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof Date) {
			return (Date) obj;
		}

		String objStr = obj.toString();
		if (StringUtil.isBlank(objStr)) {
			return null;
		}

		try {
			Date date = sdf1.parse(objStr);
			return date;
		} catch (ParseException e) {
			return null;
		}

	}

	public static Map getMap(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof Map) {
			return (Map) obj;
		}

		throw new ClassCastException("无法转换成Map对象");
	}

	public static List getArray(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof List) {
			return (List) obj;
		}

		throw new ClassCastException("无法转换成List对象");
	}

	public static BigInteger getBigInteger(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof BigInteger) {
			return (BigInteger) obj;
		}

		String objStr = obj.toString();
		if (StringUtil.isBlank(objStr)) {
			return null;
		}

		try {
			return new BigInteger(objStr);
		} catch (Exception e) {
			return null;
		}
	}

	public static String getString(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof String) {
			return (String) obj;
		}

		return obj.toString();
	}

	public static BigDecimal getBigDecimal(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof BigDecimal) {
			return (BigDecimal) obj;
		}

		String strObj = obj.toString();
		if (StringUtil.isBlank(strObj)) {
			return null;
		}

		try {
			return new BigDecimal(strObj);
		} catch (Exception e) {
			return null;
		}
	}

	public static Long getLong(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof Long) {
			return (Long) obj;
		}

		String objStr = obj.toString();
		if (StringUtil.isBlank(objStr)) {
			return null;
		}

		try {
			return new Long(objStr);
		} catch (Exception e) {
			return null;
		}
	}

	public static Integer getInteger(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof Integer) {
			return (Integer) obj;
		}

		String objStr = obj.toString();
		if (StringUtil.isBlank(objStr)) {
			return null;
		}

		try {
			return new Integer(objStr);
		} catch (Exception e) {
			return null;
		}
	}

	public static Boolean getBoolean(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof Boolean) {
			return (Boolean) obj;
		}

		String objStr = obj.toString();
		if (StringUtil.isBlank(objStr)) {
			return null;
		}

		return Boolean.valueOf(objStr);
	}

	public static Double getDouble(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof Double) {
			return (Double) obj;
		}

		String strObj = obj.toString();
		if (StringUtil.isBlank(strObj)) {
			return null;
		}

		try {
			return Double.valueOf(strObj);
		} catch (Exception e) {
			return null;
		}
	}

	public static Float getFloat(Map map, String key) {

		Object obj = map.get(key);
		if (obj == null) {
			return null;
		}

		if (obj instanceof Double) {
			return (Float) obj;
		}

		String strObj = obj.toString();
		if (StringUtil.isBlank(strObj)) {
			return null;
		}

		try {
			return Float.valueOf(strObj);
		} catch (Exception e) {
			return null;
		}
	}

	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<>();
		map.put("db", "123456.er4");
		System.out.println(MapUtil.getBigDecimal(map, "db"));
		System.out.println(MapUtil.getDouble(map, "db"));
		System.out.println(MapUtil.getFloat(map, "db"));
	}
}
